

"""日志"""
import logging
from Tools.logtools.logtools import logfile_hander, console_handler
from common.models import framemenu
from mysqltools.MysqlTool import MysqlTool

logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
logger.addHandler(logfile_hander())
logger.addHandler(console_handler())

'''加载yaml文件'''
#^_^ 2024-12-18-shp 修改yaml
import  ruamel.yaml
def demo_config():
    """
    加载全局配置文件
    :return:
    """
    # 创建YAML对象，并指定加载类型为 'rt'（即 Round-Trip，保持原始格式）
    yaml = ruamel.yaml.YAML(typ='rt')
    #os.chdir(os.path.dirname(os.path.abspath(__file__)))
    with open("../Tools/config/config.yaml", "r") as smallfile:
        config = yaml.load(smallfile)  # pprint(config)
    return config
cfg = demo_config()


def get_framemenu(permission_level:int =1):
    """
    2025-04-29 shp 查询联合查询common_parentmenu和common_framemenu
    :param permission_level 权限等级
    :return:要查询的链接
    """
    #high_permission_menus = framemenu.objects.filter(permission_level__gte=permission_level)
    try:
        mysql = dict(cfg["doumo"]["mysql"]["database"][0])
        db = MysqlTool(ip=cfg["doumo"]["ip"], database=mysql["dbname"], user=mysql["user"], pwd=mysql["pwd"],
                       port=cfg["doumo"]["mysql"]["port"], )

        sql_query = f"""
            SELECT
            cf.title,
            cf.menu_id,
            cf.`path` ,
            cp.fratitle,
            cp.id
        from
            common_framemenu cf
        join common_parentmenu cp on
            cp.id = cf.parent_id
        where 
	        {permission_level} >= cf.permission_level 
        order by
            cp.sort_order,
            cf.sort_order
        """

        rows_cst = db.query_all(sql_query)
        return rows_cst

    except KeyError as e:
        logger.error(f"配置项缺失: {str(e)}")
        raise RuntimeError("系统配置错误") from e
    except Exception as e:
        logger.error(f"数据库操作异常: {str(e)}", exc_info=True)
        raise

